#pragma once

#include"HashTable.h"

namespace lh
{
	template<class K, class V, class Hash = HashFunc<K>>
	class unordered_map
	{
		struct MapKeyOfT
		{
			const K& operator()(const pair<K, V>& kv)
			{
				return kv.first;
			}
		};

	public:
		typedef typename Bucket::HashTable<K, pair<K, V>, Hash, MapKeyOfT>::iterator iterator;
		iterator begin()
		{
			return _ht.begin();
		}
		iterator end()
		{
			return _ht.end();
		}

		pair<iterator,bool> insert(const pair<K, V>& kv)
		{
			return _ht.Insert(kv);
		}
		//返回值（第二个模板V:k or pair，只能是pair，因为set没有[]）的引用
		V& operator[](const K& key)
		{
			pair<iterator,bool> ret = _ht.Insert(make_pair(key, V()));
			return ret.first->second;
		}


	private:
		Bucket::HashTable<K, pair<K, V>, Hash, MapKeyOfT>_ht;
	};

	void Test_map()
	{
		unordered_map<string, string> mp;
		mp.insert(make_pair("string", "字符串"));
		mp.insert(make_pair("left", "左部"));
		mp.insert(make_pair("right", "右部"));
		unordered_map<string, string>::iterator it = mp.begin();
		while (it != mp.end())
		{
			cout << it->first << ":" << it->second << endl;
			++it;
		}
		cout << endl;

		unordered_map<string, int>countMap;
		string arr[] = { "苹果","苹果","西瓜","苹果","香蕉","香蕉","西瓜","苹果","香蕉" };
		for (auto e : arr)
		{
			countMap[e]++;
		}
		for (auto ch : countMap)
		{
			cout << ch.first << ":" << ch.second << endl;
		}
		cout << endl;


	}


}
